<template>
  <div class="body">
    <el-container style="height: 100%">
      <el-side :style="sideStyle">
        <el-menu router :collapse="collapse" :collapse-transition="false" :default-active="defaultActive">
          <el-menu-item index="echarts">
            <i class="el-icon-s-home"></i>
            <span slot="title">博客后台</span>
          </el-menu-item>
          <el-menu-item index="apply">
            <i class="el-icon-edit-outline"></i>
            <span slot="title">发布文章</span>
          </el-menu-item>
          <el-menu-item index="article">
            <i class="el-icon-set-up"></i>
            <span slot="title">文章列表</span>
          </el-menu-item>
          <el-menu-item index="category">
            <i class="el-icon-paperclip"></i>
            <span slot="title">类型列表</span>
          </el-menu-item>
          <el-menu-item index="comment">
            <i class="el-icon-chat-dot-square"></i>
            <span slot="title">评论列表</span>
          </el-menu-item>
          <el-submenu index="manager">
            <template slot="title">
              <i class="el-icon-s-tools"></i>
              <span>系统管理</span>
            </template>
            <el-menu-item index="user">
              <i class="el-icon-user-solid"></i>
              <span slot="title">用户管理</span>
            </el-menu-item>
            <el-menu-item index="banner">
              <i class="el-icon-data-analysis"></i>
              <span slot="title">轮播管理</span>
            </el-menu-item>
            <el-menu-item index="href">
              <i class="el-icon-link"></i>
              <span slot="title">外链管理</span>
            </el-menu-item>
            <el-menu-item index="website">
              <i class="el-icon-s-platform"></i>
              <span slot="title">网站信息</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-side>
      <el-container>
        <el-header style="height: 56px">
          <el-row style="height: 100%">
            <el-col :span="1" class="header-icon">
              <i :class="iconClass" style="font-size:18px;cursor: pointer;" :title="iconText" @click="changeCollapse"></i>
            </el-col>
            <el-col :span="4" :offset="19" class="header-info">
              <div style="margin-right: 10px;">
                <span>当前用户：</span>
                <el-tag effect="dark" style="cursor: pointer">{{ loginInfo.nickname }}</el-tag>
              </div>
              <el-dropdown trigger="click">
                <div style="height: 50px;width: 50px;overflow: hidden;border-radius: 50%;margin-right: 15px;">
                  <img :src="imagePath+'img/'+loginInfo.avatar" style="display: block;height: 100%;width: 100%;" alt="">
                </div>
                <el-dropdown-menu>
                  <el-dropdown-item @click.native="openWin">个人信息</el-dropdown-item>
                  <el-dropdown-item @click.native="openPassword">修改密码</el-dropdown-item>
                  <el-dropdown-item @click.native="logout" divided>退出系统</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-header>
        <el-main style="background-color: whitesmoke">
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
    <UserUpdate ref="userUpdate" @closeWin="loginInfo"/>
    <UserPassword ref="userPassword" @closeWin="toLogin"/>
  </div>
</template>

<script>
import UserUpdate from '@/views/admin/user/UserUpdate.vue'
import UserPassword from '@/views/admin/user/UserPassword.vue'

export default {
  name: 'AdminView',
  components: { UserUpdate, UserPassword },
  data () {
    return {
      collapse: false,
      sideStyle: {
        width: '200px'
      },
      iconText: '收起',
      iconClass: 'el-icon-s-fold',
      defaultActive: 'echarts',
      loginInfo: {},
      imagePath: process.env.VUE_APP_API
    }
  },
  watch: {
    $route (to) {
      this.setDefaultActive(to.path)
    }
  },
  methods: {
    changeCollapse () {
      this.collapse = !this.collapse
      this.sideStyle.width = this.collapse ? '65px' : '200px'
      this.iconText = this.collapse ? '打开' : '收起'
      this.iconClass = this.collapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'
    },
    setDefaultActive (path) {
      const ps = path.split('/')
      this.defaultActive = ps[ps.length - 1]
    },
    openWin () {
      this.$refs.userUpdate.init(this.loginInfo.userId)
    },
    openPassword () {
      this.$refs.userPassword.init(this.loginInfo.username)
    },
    logout () {
      this.$confirm('确定退出系统吗？').then(() => {
        this.$http.post('admin/logout').then(() => {
          this.toLogin()
        })
      })
    },
    toLogin () {
      this.$cookies.remove('token')
      this.$router.replace('/login')
    },
    getLoginInfo () {
      this.$http.get('admin/user/login/info').then(data => {
        this.loginInfo = data.data
      })
    }
  },
  created () {
    this.setDefaultActive(this.$route.path)
    this.getLoginInfo()
  }
}
</script>
<style>
.el-menu{
  border: none;
}
</style>
<style scoped lang="scss">
.body{
  height: 100vh;
  width: 100vw;
}
.header-icon{
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.header-info{
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: right;
}
</style>
